<?php
/*
 * *********************************************************
 * ## Module controllers must extend MD_Controller class ##
 * *********************************************************
 */
class Home extends MD_Controller {
	
	static $pathToNumbers = 'numbers/';
		
	static $csvField = 'upload_csv';
	static $xlsField = 'upload_xls';
    
public function __construct()
{
		
	
    parent::__construct();
    
			/*
			* ******************************************************************
			* CHECK IF MODULE IS INSTALLED PROPERLY
			* ******************************************************************
			*/
			$this->MODULE->config->load('module_config');	
			$short_name = $this->MODULE->config->item('short_name');
			$settings_table = $this->MODULE->config->item('settings_table');	 
			if(!$this->PACKAGE->security->app_modules->module_exist($short_name) || empty($short_name) || empty($settings_table))
			{
				$this->PACKAGE->errors->generate->code("001")->stop();
			}
    
			/*
				* ********************************************
				* LOAD LANGUAGE FILES FOR THIS MODULE ONLY
				* notice that some language files have been
				* loaded within MY_Controller core class
				* and those language files are common for all
				* modules
				* ********************************************
				*/    

			$this->MODULE->lang->load('module_texts',LANGUAGE);

			/*
				* ********************************************
				* LOAD PACKAGES
				* ********************************************
				*/
			$this->PACKAGE->core->packages->add('regional');   
			$this->PACKAGE->core->packages->add('routing');   
      
			/*
			 * ********************************************
			 * LOAD LIBRARIES
			 * ********************************************
			 */

			/*
				* ********************************************
				* LOAD MODELS
				* ********************************************
				*/
			$this->PACKAGE->regional->load->model('countries');
			$this->PACKAGE->regional->load->model('timezone');
						
			$this->MODULE->load->model('module_mod_ratesheets_purchase');
			
			//Settings for upload file
			$config['upload_path'] = UPLOAD_PATH . static::$pathToNumbers;
			$config['allowed_types'] = 'csv|xlsx|xls'; // only csv and xls
			$config['max_size']	= '0'; // no limit
			// Load upload lybrary
			$this->load->library('upload', $config);
			// helper
			$this->load->helper('file');
		
  
	}
    
	public function index()
	{
	        
			$data = array(
										'title'       => lang('ratesheets_purchase_home_title'),
										'description' => lang('ratesheets_purchase_home_desc')
										);
			
			
			if(isset($_FILES))
			{
				$post = $this->input->post();

				$data['received_date'] = (!empty($post['received-date'])) ? $post['received-date'] : null; // get date
				
				$data['upload_data_xls'] = $this->module_mod_ratesheets_purchase->uploadXLSfile(self::$xlsField); // for xls
				$data['upload_data_csv'] = $this->module_mod_ratesheets_purchase->uploadCSVfile(self::$csvField); // for csv
				
				if(false === $data['upload_data_xls']['upload_xls']) // data is incorrect
				{
					
					$data = $this->_resetValues($data);
					$data['upload_error'] = $data['upload_data_xls']['upload_msg_xls'];
					$data['upload_success'] = false;
						
				} elseif( false === $data['upload_data_csv']['upload_csv']) // data is incorrect
				{ 

					$data = $this->_resetValues($data);
					$data['upload_error'] = $data['upload_data_csv']['upload_msg_csv'];
					$data['upload_success'] = false;

				} elseif(empty($data['received_date']))  // forgot to select date
				{ 
					$data = $this->_resetValues($data);
					
					$data['upload_error'] = lang('data_not_selected');
					$data['upload_success'] = false;
					
				} else {
					$data['upload_error'] = false;
					$data['upload_success'] = $data['upload_data_csv']['upload_msg_csv'] . '<br/>' . $data['upload_data_xls']['upload_msg_xls'];
				}
				
			}
			
		
			/*
			$this->APPLICATION->app_links->path = array(
												MODULE_SITE_URL => lang('term_numbering')
												);
			*/

			//  parse templates

			$this->load->view('header',$data); // shared header

			$this->MODULE->load->view('module_header'); // module header    

			$this->load->view('topmenu'); // theme sample template          

			$this->MODULE->load->view('module_home'); // ratesheets_purchase template              

			$this->MODULE->load->view('module_footer'); // module footer

			$this->load->view('footer'); // shared footer
	}
	
	/**
	 * Reset Values Data (for xls and csv), if one condition is FALSE
	 * @param array $data
	 * @return array 
	 */
	private function _resetValues($data)
	{
		if(isset($data['upload_data_xls']['full_path_xls']))
			{
				unlink($data['upload_data_xls']['full_path_xls']); // delete file
			} else {}

		if(isset($data['upload_data_csv']['upload_csv_data']))
		{
			$data['upload_data_csv']['upload_csv_data'] = 0;  // delete all numbers
		} else {}

		
		return $data;
	}
    
} // end of class
?>